home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / sys / sysPrintf.c < prev    next >
C/C++ Source or Header  |  1990-12-06  |  6KB  |  305 lines

  1. /*
  2.  *  sysPrintf --
  3.  *
  4.  *      Perform all formatted printing to the console.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/kernel/sys/RCS/sysPrintf.c,v 9.12 90/12/06 17:36:58 shirriff Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include <sprite.h>
  22. #include <machMon.h>
  23. #include <sync.h>
  24. #include <mach.h>
  25. #include <main.h>
  26. #include <fs.h>
  27. #include <varargs.h>
  28. #include <stdio.h>
  29. #include <sys.h>
  30. #include <sysInt.h>
  31. #include <dbg.h>
  32. #include <dev.h>
  33. #include <devVid.h>
  34.  
  35. /*
  36.  * Calls to panic and printf are protected.
  37.  */
  38. Sync_Semaphore    sysPrintMutex = Sync_SemInitStatic("sysPrintMutex");
  39.  
  40. /*
  41.  * Set during a panic to prevent recursion.
  42.  */
  43. Boolean    sysPanicing = FALSE;
  44.  
  45. /*
  46.  * Used to keep track of bytes written.
  47.  */
  48. static int bytesWritten;
  49.  
  50. static void writeProc _ARGS_((FILE *stream, Boolean flush));
  51.  
  52. /*
  53.  * vprintf buffer.
  54.  */
  55. #define    STREAM_BUFFER_SIZE    512
  56. static unsigned char streamBuffer[STREAM_BUFFER_SIZE];
  57.  
  58.  
  59. /*
  60.  * ----------------------------------------------------------------------------
  61.  *
  62.  * writeProc --
  63.  *
  64.  *      Stream writeProc - flushes data to syslog. 
  65.  *
  66.  * Results:
  67.  *    None
  68.  *
  69.  * Side effects:
  70.  *      None.
  71.  *
  72.  * ----------------------------------------------------------------------------
  73.  */
  74. /*ARGSUSED*/
  75. static void
  76. writeProc(stream, flush)
  77.     FILE *stream;
  78.     Boolean flush;
  79. {
  80.     Fs_IOParam    io;
  81.     Fs_IOReply    reply;
  82.  
  83.     bzero((char *)&io, sizeof(io));
  84.     io.buffer = (Address) stream->buffer;
  85.     io.length = stream->lastAccess + 1 - stream->buffer;
  86.     bzero((char *)&reply, sizeof(reply));
  87.  
  88.     if (io.length > 0) { 
  89.     (void)Dev_SyslogWrite((Fs_Device *) NIL, &io, &reply);
  90.     stream->lastAccess = stream->buffer - 1;
  91.     stream->writeCount = stream->bufSize;
  92.     bytesWritten += reply.length;
  93.     }
  94. }
  95.  
  96.  
  97. /*
  98.  * ----------------------------------------------------------------------------
  99.  *
  100.  * vprintf --
  101.  *
  102.  *    Printing routine that is called from varargs procedures.  The
  103.  *    caller should use to varargs macros to extract the format
  104.  *    string and the va_list structure.  This also checks for
  105.  *    recursion that can result from a panic and initializes
  106.  *    the stream data structure needed by the standard vfprintf.
  107.  *
  108.  * Results:
  109.  *      Number of characters printed.
  110.  *
  111.  * Side effects:
  112.  *      None.
  113.  *
  114.  * ----------------------------------------------------------------------------
  115.  */
  116.  
  117. #ifdef lint
  118. /* VARARGS1 */
  119. /* ARGSUSED */
  120. int vprintf(format)
  121.     char *format;
  122. {
  123.     /*
  124.      * Lint complains about unused variables...  This is all #ifdef'ed lint.
  125.      * It's silly and can probably be cut down a bit....
  126.      */
  127.     char foo;
  128.     Sync_Semaphore *barPtr;
  129.     barPtr = &sysPrintMutex;
  130.     sysPrintMutex = *barPtr;
  131.     writeProc((FILE *) NULL, 0);
  132.     streamBuffer[0] = '\0';
  133.     foo = streamBuffer[0];
  134.     streamBuffer[0] = foo;
  135. }
  136. #else
  137. /*VARARGS1*/
  138. int
  139. vprintf(format, args)
  140.     char    *format;
  141.     va_list    args;
  142. {
  143.     static Boolean    initialized = FALSE;
  144.     static FILE        stream;
  145.     static int    recursiveCallP = 0;    /* prevent recursive calls
  146.                      * that could occur if vprintf
  147.                      * fails, etc.  */
  148.  
  149.     if (recursiveCallP != 0) {
  150.     return 0;
  151.     }
  152.     recursiveCallP = 1;
  153.     MASTER_LOCK(&sysPrintMutex);
  154.     if (!initialized) {
  155.     Stdio_Setup(&stream, 0, 1, streamBuffer, STREAM_BUFFER_SIZE,
  156.         (void (*)()) 0, writeProc,  (int (*)()) 0, (ClientData) 0);
  157.     initialized = TRUE;
  158.     }
  159.  
  160.     bytesWritten = 0;
  161.     vfprintf(&stream, format, args);
  162.     fflush(&stream);
  163.     MASTER_UNLOCK(&sysPrintMutex);
  164.     recursiveCallP = 0;
  165.     return (bytesWritten);
  166.  
  167. }
  168. #endif
  169.  
  170. /* 
  171.  *----------------------------------------------------------------------
  172.  *
  173.  * panic --
  174.  *
  175.  *    Print an error message and enter the debugger. This entry is 
  176.  *    provided for libc.a routines.
  177.  *
  178.  * Results:
  179.  *    None.
  180.  *
  181.  * Side effects:
  182.  *    The kernel dies, entering the debugger if possible.
  183.  *
  184.  *----------------------------------------------------------------------
  185.  */
  186.  
  187. #ifdef lint
  188. /* VARARGS1 */
  189. /* ARGSUSED */
  190. void panic(format)
  191.     char *format;
  192. {}
  193. #else
  194. void
  195. panic(va_alist)
  196.     va_dcl            /* char *format, then any number of additional
  197.                  * values to be printed under the control of
  198.                  * format.  This is all just the same as you'd
  199.                  * pass to printf. */
  200. {
  201.     char *format;
  202.     va_list args;
  203.  
  204.     va_start(args);
  205.     format = va_arg(args, char *);
  206.  
  207.     if (!main_PanicOK) {
  208.     Mach_MonPrintf("Fatal Error: ");
  209.     Mach_MonPrintf(format, args);
  210.     }
  211.     Dev_VidEnable(TRUE);    /* unblank the screen */
  212.     Dev_SyslogDebug(TRUE);    /* divert /dev/syslog output to the screen */
  213.     if (!sysPanicing) {
  214.     printf("Fatal Error: ");
  215.     (void) vprintf(format, args);
  216.     va_end(args);
  217.     }
  218.     sysPanicing = TRUE;
  219.     DBG_CALL;
  220.     Dev_SyslogDebug(FALSE);
  221. }
  222. #endif
  223.  
  224. /*
  225.  * ----------------------------------------------------------------------------
  226.  *
  227.  * printf --
  228.  *
  229.  *      Perform a C style printf with disabling of interrupts.
  230.  *
  231.  * Results:
  232.  *      None.
  233.  *
  234.  * Side effects:
  235.  *      None.
  236.  *
  237.  * ----------------------------------------------------------------------------
  238.  */
  239.  
  240.  
  241. #ifdef lint
  242. /* VARARGS1 */
  243. /* ARGSUSED */
  244. void printf(format)
  245.     char *format;
  246. {}
  247. #else
  248. void
  249. printf(va_alist)
  250.     va_dcl
  251. {
  252.     char *format;
  253.     va_list    args;
  254.  
  255.     va_start(args);
  256.     format = va_arg(args, char *);
  257.  
  258.     (void) vprintf(format, args);
  259.     va_end(args);
  260. }
  261. #endif
  262.  
  263. /*
  264.  * ----------------------------------------------------------------------------
  265.  *
  266.  * fprintf --
  267.  *
  268.  *      Perform a C style fprintf with disabling of interrupts (output
  269.  *    always goes to the console:  stream arg is ignored).
  270.  *
  271.  * Results:
  272.  *      None.
  273.  *
  274.  * Side effects:
  275.  *      None.
  276.  *
  277.  * ----------------------------------------------------------------------------
  278.  */
  279.  
  280. #ifdef lint
  281. /* VARARGS */
  282. /* ARGSUSED */
  283. int fprintf()
  284. {}
  285. #else
  286. /*
  287. int
  288. fprintf(va_alist)
  289.     va_dcl
  290. {
  291.     char *format;
  292.     va_list    args;
  293.     int result;
  294.  
  295.     va_start(args);
  296.     (void) va_arg(args, FILE *);
  297.     format = va_arg(args, char *);
  298.  
  299.     result = vprintf(format, args);
  300.     va_end(args);
  301.     return result;
  302. }
  303. */
  304. #endif
  305.